package com.anivia.common.web.satoken;

import cn.dev33.satoken.config.SaTokenConfig;
import cn.dev33.satoken.filter.SaServletFilter;
import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.jwt.StpLogicJwtForSimple;
import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.stp.StpLogic;
import cn.dev33.satoken.strategy.SaStrategy;
import cn.dev33.satoken.util.SaResult;
import com.anivia.common.core.constant.CommonStant;
import com.anivia.common.web.satoken.admin.StpAdminUtil;
import com.anivia.common.web.satoken.tenant.StpTenantUtil;
import com.anivia.common.web.satoken.user.StpUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.concurrent.TimeUnit;

/**
 * Sa-Token 配置
 *
 * @author wangj
 */
@Configuration
public class SaTokenConfigure implements WebMvcConfigurer {
    /**
     * 不同用户类型使用不配的配置
     */
    @Autowired
    public void setSaTokenConfig() {
        SaTokenConfig userCfg = new SaTokenConfig();
        userCfg.setTokenName(CommonStant.AUTHORIZATION);
        userCfg.setTimeout(TimeUnit.DAYS.toSeconds(300));
        userCfg.setIsConcurrent(true);
        userCfg.setIsShare(false);
        userCfg.setMaxLoginCount(3);
        userCfg.setIsReadHeader(true);
        userCfg.setIsWriteHeader(true);
        userCfg.setTokenStyle("random-128");
        userCfg.setAutoRenew(true);
        userCfg.setIsLog(true);
        userCfg.setLogLevel("debug");
        userCfg.setIsReadCookie(false);
        userCfg.setIsColorLog(true);
        userCfg.setJwtSecretKey("23425b3444dc4fc0952cde9661669f46");
        StpUserUtil.stpLogic.setConfig(userCfg);


        SaTokenConfig adminCfg = new SaTokenConfig();
        adminCfg.setTokenName(CommonStant.AUTHORIZATION);
        adminCfg.setTimeout(TimeUnit.DAYS.toSeconds(300));
        adminCfg.setIsConcurrent(true);
        adminCfg.setIsShare(false);
        adminCfg.setMaxLoginCount(3);
        adminCfg.setIsReadCookie(false);
        adminCfg.setIsReadHeader(true);
        adminCfg.setIsWriteHeader(true);
        adminCfg.setTokenStyle("random-128");
        adminCfg.setAutoRenew(true);
        adminCfg.setIsLog(true);
        adminCfg.setLogLevel("debug");
        adminCfg.setIsColorLog(true);
        adminCfg.setJwtSecretKey("23425b3444dc4fc0952cde9661669f47");
        StpAdminUtil.stpLogic.setConfig(adminCfg);


        SaTokenConfig tenantCfg = new SaTokenConfig();
        tenantCfg.setTokenName(CommonStant.AUTHORIZATION);
        tenantCfg.setTimeout(TimeUnit.DAYS.toSeconds(300));
        tenantCfg.setIsConcurrent(true);
        tenantCfg.setIsShare(false);
        tenantCfg.setMaxLoginCount(3);
        tenantCfg.setIsReadCookie(false);
        tenantCfg.setIsReadHeader(true);
        tenantCfg.setIsWriteHeader(true);
        tenantCfg.setTokenStyle("random-128");
        tenantCfg.setAutoRenew(true);
        tenantCfg.setIsLog(true);
        tenantCfg.setLogLevel("debug");
        tenantCfg.setIsColorLog(true);
        tenantCfg.setJwtSecretKey("23425b3444dc4fc0952cde9661669f48");
        StpTenantUtil.stpLogic.setConfig(tenantCfg);

    }

    @Bean
    public SaServletFilter getSaServletFilter() {
        return new SaServletFilter().addExclude("/**").setAuth(obj -> {
            SaRouter.match("/**").notMatch("/**").check(r -> StpUserUtil.checkLogin());

        }).setError(e -> SaResult.error(e.getMessage()));
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册 Sa-Token 拦截器，打开注解式鉴权功能
        registry.addInterceptor(new SaInterceptor()).addPathPatterns("/**");
    }

    /**
     * 重写 Sa-Token 框架内部算法策略
     */
    @Autowired
    public void rewriteSaStrategy() {
        // 重写Sa-Token的注解处理器，增加注解合并功能
        SaStrategy.me.getAnnotation = (element, annotationClass) -> {
            return AnnotatedElementUtils.getMergedAnnotation(element, annotationClass);
        };
    }

    @Bean
    public StpLogic getStpLogicJwt() {
        return new StpLogicJwtForSimple();
    }
}
